package common.display
{
    import flash.display.BitmapData;
    import flash.utils.ByteArray;
    import flash.utils.Dictionary;

    public class ActionImage
    {
        public static const VESION:int = 2;
        private var _data:Vector.<ActionImageData>;

        private var directetmap:Array;

        private var _frameCount:int;
        private var _directionCount:int;

        public function ActionImage(frame:int , direct:int)
        {
            _frameCount = frame;
            _directionCount = direct;
            directetmap = new Array ();

            _data = new Vector.<ActionImageData> (direct * frame);

        }

        public static function createSetFromByte(bytes:ByteArray):ActionImage
        {

            var vesion:int = bytes.readUnsignedShort ();

            if (vesion != VESION)
                return null;

            var sets:ActionImage = new ActionImage (bytes.readUnsignedByte () , bytes.readUnsignedByte ());

            for (var i:int = 0 ; i < sets.directionCount ; i++)
            {
                sets.directetmap.push (bytes.readUnsignedShort ());
            }
            return sets;
        }

        public static function createDefaultActionImage(image:BitmapData):ActionImage
        {
            var sets:ActionImage = new ActionImage (1 , 1);
            var actionimagedata:ActionImageData = new ActionImageData ();
            actionimagedata.images = image;
            actionimagedata.defautl ();
            sets.setActionImage (actionimagedata);
            return sets;
        }

        public function setActionImage(aci:ActionImageData):void
        {
            _data[aci.direct * frameCount + aci.frame] = aci;
        }

        public function get directionCount():int
        {
            return _directionCount;
        }

        public function get frameCount():int
        {
            return _frameCount;
        }

        public function getIndexDirection(direct:int):int
        {
            direct = (direct + 360) % 360;
            var len:int = directetmap.length;
            for (var index:int = 0 ; index < len ; index++)
            {
                if (directetmap[index] > direct)
                {
                    return Math.max (index - 1 , 0);
                }
            }
            return 0;
        }

        public function getActionImageByDirectionAndFrame(direct:int , frame:int):ActionImageData
        {
            var d:int = getIndexDirection (direct);

            return _data[d * frameCount + frame];
        }

        public function getActionImageByIndex(index:int):ActionImageData
        {
            if (index < _data.length)
            {
                return _data[index];
            }
            return null;
        }

        public function destory():void
        {

            var len:int = directionCount * frameCount;
            for (var i:int = 0 ; i < len ; i++)
            {
                _data[i].destory ();
            }
        }
    }
}
